//
//  DrawTestView.m
//  CALayerTest
//
//  Created by ficow on 4/14/16.
//  Copyright © 2016 ficow. All rights reserved.
//

#import "DrawTestView.h"

@implementation DrawTestView

- (void)drawRect:(CGRect)rect {
    
    [self locationTransition];
}

- (void)defaultShapeDraw{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ctx, 16);
    CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);
    
    //straight line
    const CGPoint points1[] = {CGPointMake(10, 20),CGPointMake(100, 20),CGPointMake(100, 20),CGPointMake(20, 50)};
    CGContextStrokeLineSegments(ctx, points1, 4);
    
    CGContextSetLineCap(ctx, kCGLineCapSquare);
    const CGPoint points2[] = {CGPointMake(110, 20),CGPointMake(200, 20),CGPointMake(200, 20),CGPointMake(120, 50)};
    CGContextStrokeLineSegments(ctx, points2, 4);
    
    CGContextSetLineCap(ctx, kCGLineCapRound);
    const CGPoint points3[] = {CGPointMake(210, 20),CGPointMake(300, 20),CGPointMake(300, 20),CGPointMake(220, 50)};
    CGContextStrokeLineSegments(ctx, points3, 4);
    
    //dashed line
    CGContextSetLineCap(ctx, kCGLineCapButt);
    CGContextSetLineWidth(ctx, 10);
    CGFloat patterns1[] = {6,10};
    CGContextSetLineDash(ctx, 0, patterns1, 1);
    
    const CGPoint points4[] = {CGPointMake(40, 65),CGPointMake(280, 65)};
    CGContextStrokeLineSegments(ctx, points4, 2);
    
    CGContextSetLineDash(ctx, 3, patterns1, 1);
    const CGPoint points5[] = {CGPointMake(40, 85),CGPointMake(280, 85)};
    CGContextStrokeLineSegments(ctx, points5, 2);
    
    CGFloat patterns2[] = {5,1,4,1,3,1,2,1,1,1,1,2,1,3,1,4,1,5};
    CGContextSetLineDash(ctx, 0, patterns2, 18);
    const CGPoint points6[] = {CGPointMake( 40, 105),CGPointMake(280, 105)};
    CGContextStrokeLineSegments(ctx, points6, 2);
    
    //rectangle
    CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
    CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
    CGContextSetLineWidth(ctx, 14);
    CGContextSetLineDash(ctx, 0, 0, 0);//cancel dashed style
    CGContextStrokeRect(ctx, CGRectMake(30, 230, 120, 60));
    
    CGContextSetStrokeColorWithColor(ctx, [UIColor purpleColor].CGColor);
    CGContextSetLineJoin(ctx, kCGLineJoinRound);//round corner
    CGContextStrokeRect(ctx, CGRectMake(80, 260, 120, 60));
    
    CGContextSetRGBStrokeColor(ctx, 1.0, 0, 1.0, 1);
    CGContextSetLineJoin(ctx, kCGLineJoinBevel);//cliped corner
    CGContextStrokeRect(ctx, CGRectMake(130, 290,120, 60));
    
    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);//filled rectangle
    CGContextFillRect(ctx, CGRectMake(30,120, 120, 60));
    
    //oval
    CGContextSetRGBStrokeColor(ctx, 0, 1, 1, 1);
    CGContextStrokeEllipseInRect(ctx, CGRectMake(30, 380, 120, 60));//Stroke oval
    
    CGContextSetRGBFillColor(ctx, 1, 0, 1, 1);
    CGContextFillEllipseInRect(ctx, CGRectMake(180, 380, 120, 60));//Filled oval
    
}

- (void)customShapeDraw{
    CGContextRef ctx = UIGraphicsGetCurrentContext();//get context
    for (int i = 0; i < 10; i++) {
        CGContextBeginPath(ctx);
        CGContextAddArc(ctx, i * 35, i * 35, (i + 1) * 6, 0 , 1.5 * M_PI, 0);//actually 0 means clockwise
        CGContextClosePath(ctx);
        CGContextSetRGBFillColor(ctx, 1, 0, 1, (10 - i) * 0.1);
        CGContextFillPath(ctx);
    }
}

- (void)drawRoundRect{
    CGContextRef ctx = UIGraphicsGetCurrentContext();//get context
    
    CGContextBeginPath(ctx);
    
    CGFloat x,y,radius,width,height;
    x = 50; y = 50; radius = 10; width = 90; height = 40;
    CGContextMoveToPoint(ctx, x + radius, y);//move to left top corner
    CGContextAddLineToPoint(ctx, x + width - radius, y);//add top line to right top corner
    CGContextAddArcToPoint(ctx, x + width, y, x + width, y + radius, radius);//add right top corner
    CGContextAddLineToPoint(ctx, x + width, y + height - radius);//add right line to right bottom corner
    CGContextAddArcToPoint(ctx, x + width, y + height, x + width - radius, y + height, radius);//add right bottom corner
    CGContextAddLineToPoint(ctx, x + radius, y + height);//add bottom line to left bottom corner
    CGContextAddArcToPoint(ctx, x, y + height, x, y + height - radius, radius);//add left bottom corner
    CGContextAddLineToPoint(ctx, x, y + radius);//add left line to left top corner
    CGContextAddArcToPoint(ctx, x, y, x + radius, y, radius);//add left top corner
    
    CGContextClosePath(ctx);
    CGContextSetRGBFillColor(ctx, 1, 0, 1, 0.6);
    CGContextFillPath(ctx);
}

- (void)drawCustomStar{
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();//get context
    
    CGContextBeginPath(ctx);
    
    CGFloat x,y,size;
    NSInteger nCorner = 5;//n corner
    x = 150; y = 350; size = 50;
    CGFloat dig = 4 * M_PI / nCorner;CGContextMoveToPoint(ctx, x, y + size);
    for (int i = 1; i <= nCorner; i++) {
        CGFloat _x = sin(i * dig);
        CGFloat _y = cos(i * dig);
        CGContextAddLineToPoint(ctx, _x * size + x, _y * size + y);
    }
    
    CGContextClosePath(ctx);
    CGContextSetRGBFillColor(ctx, 1, 0, 1, 0.6);
    CGContextFillPath(ctx);
}

- (void)drawFlower{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGContextBeginPath(ctx);
    
    CGFloat x,y,size,length;
    NSInteger nCorner = 6;//n corner
    x = 150; y = 350; size = 30; length = 120;//length should be bigger
    CGContextMoveToPoint(ctx, x, y + size);
    CGFloat dig = 2 * M_PI / nCorner;
    for (int i = 1; i < nCorner + 1; i++) {
        //count control point
        CGFloat ctrlX = sin((i - 0.5) * dig) * length + x;
        CGFloat ctrlY = cos((i - 0.5) * dig) * length + y;
        
        //count end point
        CGFloat _x = sin(i * dig) * size + x;
        CGFloat _y = cos(i * dig) * size + y;
        //draw line
        CGContextAddQuadCurveToPoint(ctx, ctrlX, ctrlY, _x, _y);
    }
    CGContextClosePath(ctx);
    CGContextSetRGBFillColor(ctx, 1, 0, 1, 0.6);
    CGContextFillPath(ctx);
}

- (void)locationTransition{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGContextTranslateCTM(ctx, 10, 200);//move the coordinate
    for (int i = 0; i < 50; i++) {
        CGContextSetRGBFillColor(ctx, 1, 0, 1, 0.3 - i * 0.01);
        CGContextFillRect(ctx, CGRectMake(0, 0, 150, 75));
        CGContextTranslateCTM(ctx, 50, 50);
        CGContextScaleCTM(ctx, 0.93, 0.93);
        CGContextRotateCTM(ctx, - M_PI / 10);
        
    }
}

@end
